<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>倒叙</title>
</head>

<body>
    <table>
        <thead>
            <tr>
                <th>姓名</th>
                <th>分数</th>
            </tr>
        </thead>
        <tbody>

        </tbody>
    </table>
</body>
<script>
    window.onload = function() {
            var scores = [{
                name: "翠花",
                score: 100
            }, {
                name: "二狗",
                score: 80
            }, {
                name: "大树",
                score: 90
            }, {
                name: "锤子",
                score: 10
            }, {
                name: "大锤",
                score: 120
            }, {
                name: "大大锤",
                score: 150
            }];

            var trueNextSlib = function(dom) {
                var brother = dom;
                do {
                    brother = brother.nextSibling;
                } while (brother != null && brother.nodeType != 1); //这个东西在什么情况下会出错*/
                return brother
            }
            var insertAfter = function(dom, oldDom, parent) {
                var brother = trueNextSlib(oldDom);
                if (brother) {
                    parent.insertBefore(dom, brother);
                } else {
                    parent.appendChild(dom);
                }
            }
            var tbody = document.querySelector("tbody");
            for (var i = 0; i < scores.length; i++) {
                var tr = document.createElement("tr");
                var tdName = document.createElement("td");
                tdName.textContent = scores[i].name;
                var tdscore = document.createElement("td");
                tdscore.textContent = scores[i].score;
                //tbody.appendChild(tr);
                tr.appendChild(tdName);
                tr.appendChild(tdscore);

                if (i == 0) {
                    tbody.appendChild(tr);
                } else {
                    var trs = document.querySelectorAll("tbody tr");
                    for (var j = trs.length - 1; j >= 0; j--) {

                        var score = Number(trs[j].querySelectorAll("td")[1].textContent);
                        var nowScore = scores[i].score;
                        if (score > nowScore) {
                            insertAfter(tr, trs[j], tbody);
                            break;
                        }
                    }
                    if (j < 0) {
                        tbody.insertBefore(tr, trs[0]);
                    }
                }
            }

        }
        //倒叙
</script>

</html>